﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ESoft.Core.Utils.Extensions;
using Newtonsoft.Json;

namespace ESoft.Core.Util.Helpers
{
    public class RequestMapHelper
    {
        public static Dictionary<string, object> GetRequestMap(Dictionary<string, object> dict, Stream stream)
        {
            if (dict.IsNull())
            {
                StreamReader reader = new StreamReader(stream);
                stream.Position = 0;
                string content = reader.ReadToEnd();
                dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
            }
            return dict;
        }
        public static Dictionary<string, object> GetRequestFilterMap(Dictionary<string, object> dict, string prefix)
        {
            var theRet = new Dictionary<string, object>();
            dict.Keys.Where(o => o.StartsWith(prefix)).ToList().ForEach(o =>
            {

                if (dict[o] == null)
                {
                    theRet.Add(o, null);
                }
                else
                {
                    theRet.Add(o, dict[o].ToString());
                }

            });
            return theRet;
        }

        public static T GetRequestMap<T>(Dictionary<string, object> dict, string queryName, T defaultValue)
        {
            if (dict.Keys.Contains(queryName))
            {

                var type = typeof(T);
                if (type.Name == "String" || type.IsPrimitive)
                {
                    return (T)Convert.ChangeType(dict[queryName], type);
                }
                else
                {
                    var value = dict[queryName].ToString();
                    var model = JsonConvert.DeserializeObject<T>(value);
                    return model;
                }

            }
            return defaultValue;
        }

        public static T GetMapModel<T>(Dictionary<string, object> dict) where T : class, new()
        {
            var type = typeof(T);
            var value = dict[type.Name];

            if (value == null)
            {
                return null;
            }
            T model = JsonConvert.DeserializeObject<T>(value.ToString());
            return model;
        }

        public static T GetMapUpdateModel<T>(Dictionary<string, object> dict, T oldModel) where T : class, new()
        {
            if (oldModel.IsNull())
            {
                return null;
            }
            var pi = typeof(T).GetProperties();
            foreach (var name in dict.Keys)
            {
                var p = pi.FirstOrDefault(o => o.Name.ToLower() == name.ToLower());
                oldModel.SetModelValue(p, dict[name]);
            }
            return oldModel;
        }

        public static List<T> GetMapModels<T>(object value) where T : class, new()
        {
            if (value == null)
            {
                return new List<T>();
            }
            var models = JsonConvert.DeserializeObject<List<T>>(value.ToString());
            return models;
        }
    }
}

